package com.atguigu.crowd.controller;


import atguigu.crowd.util.ResultEntity;
import com.atguigu.crowd.api.MySQLRemoteService;
import com.atguigu.crowd.api.RedisRemoteService;
import com.atguigu.crowd.config.VO.AddressVO;
import com.atguigu.crowd.config.VO.MemberLoginVO;
import com.atguigu.crowd.config.VO.OrderProjectVO;
import com.atguigu.crowd.config.VO.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@Slf4j
public class OrderController {

    @Autowired
    private MySQLRemoteService mySQLRemoteService;

    @GetMapping("/order/confirm/return/info/{projectId}/{returnId}")
    public String supportProduct(@PathVariable("projectId")Integer projectId, @PathVariable("returnId")Integer returnId,
                                 HttpSession session){

        ResultEntity<OrderProjectVO> orderProjectVORemote = mySQLRemoteService.getOrderProjectVORemote(projectId, returnId);
        if(ResultEntity.SUCCESS.equals(orderProjectVORemote.getResult())){
            OrderProjectVO orderProjectVO = orderProjectVORemote.getData();

            // 为了能够在后续操作中保持orderProjectVO数据，存入session域
            session.setAttribute("orderProjectVO", orderProjectVO);
        }
        return "confirm-settle";
    }

    @RequestMapping("/order/confirm/{returnCount}")
    public String showConfirmOrderInfo(@PathVariable("returnCount")Integer returnCount, HttpSession session){
        // 1.把接收到的回报数量合并到Session域中
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute("orderProjectVO");
        orderProjectVO.setReturnCount(returnCount);

        session.setAttribute("orderProjectVO", orderProjectVO);

        // 2.获取当前已经登录的用户ID
        MemberLoginVO member = (MemberLoginVO) session.getAttribute("username");
        Integer id = member.getId();

        // 3.查询目前的收获地址数据
        ResultEntity<List<AddressVO>> addressVORemote = mySQLRemoteService.getAddressVORemote(id);

        if(ResultEntity.SUCCESS.equals(addressVORemote.getResult())) {
            List<AddressVO> addressVOList = addressVORemote.getData();
            session.setAttribute("addressVOList", addressVOList);

            log.info("===============");
            for (AddressVO addressVO : addressVOList) {
                log.info(String.valueOf(addressVO));
            }
            log.info("===============");
        }
        return "confirm-order";
    }

    @PostMapping("/order/save/address")
    public String saveAddressMessage(AddressVO addressVO, HttpSession session){
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute("orderProjectVO");
        Integer returnCount = orderProjectVO.getReturnCount();

        mySQLRemoteService.saveAddressRemote(addressVO);

        return "redirect:http://localhost/order/confirm/" + returnCount;
    }

    @PostMapping("/order/pay/generate/order")
    public String paymentBill(OrderVO orderVO, HttpSession session){
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute("orderProjectVO");

        orderVO.setOrderProjectVO(orderProjectVO);

        // 生成订单号
        String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String user = UUID.randomUUID().toString().replace("-", "").toUpperCase();
        // 组装字符串为订单号
        String orderNum = time + user;

        return null;
    }
}
